﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using DataEntity;
using JScripter;
using BusinessLogic.Controller;

public partial class Admin_AjaxPages_CountryAdd :AjaxPage
{
    private int ID
    {
        get
        {
            if (Request.Params["coid"] != null || AjaxState.ContainsKey("coid"))
            {
                if (!AjaxState.ContainsKey("coid"))
                    AjaxState["coid"] = Request.Params["coid"];
                return Convert.ToInt32(AjaxState["coid"]);
            }
            else
            {
                return 0;
            }
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        if (ID > 0)
        {
            BindData();
        }
        base.OnLoad(e);
    }
    private void AddData()
    {
        try
        {
            Country objCountry = new Country();

            

            objCountry.Name = HtmlHelper.ControlValue(txtName.ClientID);

            objCountry.ModifiedDate = DateTime.Now;

            new CountryController().Add(objCountry);
            Response.Redirect("~/Admin/AjaxPages/CountryList.aspx");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = "<div class='error'>" + ex.Message + "</div>";
        }
    }

    private void EditData()
    {

        Country objCountry = new Country();

        objCountry.CountryId = ID;

        objCountry.Name = HtmlHelper.ControlValue(txtName.ClientID);

        objCountry.ModifiedDate = DateTime.Now;

        new CountryController().UpdateByCountryId(objCountry);
        Response.Redirect("~/Admin/AjaxPages/CountryList.aspx");
    }

    private void BindData()
    {
        var dataBunch = new CountryController().GetbyCountryId(ID);
        if (dataBunch.Count > 0)
        {
            var data = dataBunch[0];

            
            txtName.Text = data.Name;

            

        }
    }

    protected void AddAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        AddData();

    }
    protected void UpdateAjaxClick(object sender, AjaxControl.AjaxEventArg e)
    {
        EditData();
    }
    public void PreventData()
    {
        txtName.Text = HtmlHelper.ControlValue(txtName.ClientID);
       

    }

}